package com.sfac.javaSpringBoot.modules.account.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sfac.javaSpringBoot.modules.account.dao.RoleResourceDao;
import com.sfac.javaSpringBoot.modules.account.entity.Resource;
import com.sfac.javaSpringBoot.modules.account.service.ResourceService;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.time.LocalDateTime;

import com.sfac.javaSpringBoot.modules.account.dao.ResourceDao;

import com.sfac.javaSpringBoot.modules.account.entity.RoleResource;

@Service
public class ResourceServiceImpl implements ResourceService {
//    @Autowired
//    private EntityManager entityManager;
//    @Autowired
//    private ResourceRepository resourceRepository;
//
//
//    @Override
//    @Transactional
//    public Result<Resource> insertResource(List<Resource> resource) {
//        resource.stream().forEach(item->{
//            entityManager.persist(item);
//        });
//        entityManager.flush();
//        entityManager.clear();
//        return new Result<>(Result.ResultCode.SUCCESS.code,"" +
//                "insertUsers success");
//    }
//
//    @Override
//    @Transactional
//    public Result<Resource> deleteResource(int id) {
//        resourceRepository.deleteById(id);
//        return new Result<>(Result.ResultCode.SUCCESS.code,"" +
//                "deleteResource success");
//    }
//
//    @Override
//    @Transactional
//    public Result<Resource> editResource(Resource resource) {
//        resourceRepository.saveAndFlush(resource);
//        return new Result<>(Result.ResultCode.SUCCESS.code,"" +
//                "editResource success");
//    }
//
//    @Override
//    public List<Resource> getAllResource() {
//       return Optional.ofNullable(resourceRepository.findAll()).orElse(Collections.emptyList());
//    }
//
//    @Override
//    public Resource getResourceById(int id) {
//        return resourceRepository.findById(id).orElse(null);
//    }
//
//    @Override
//    public PageInfo<Resource> getResourcesBySearchBean(SearchBean searchBean) {
//        searchBean.initSearchBean();
//        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
//        return new PageInfo<Resource>(Optional
//                .ofNullable(resourceRepository.getResourcesBySearchBean(searchBean))
//                .orElse(Collections.emptyList()));
//    }


    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private RoleResourceDao roleResourceDao;

    @Override
    @Transactional
    public Result<Resource> insertResource(Resource resource) {
        resource.setCreateDate(LocalDateTime.now());
        resourceDao.insertResource(resource);
        if (resource.getRoles() != null) {
            resource.getRoles().stream()
                    .forEach(item -> {
                        roleResourceDao.insertRoleResource(new RoleResource(item.getId(), resource.getId()));
                    });
        }
        return new Result<Resource>(Result.ResultCode.SUCCESS.code, "Insert success", resource);
    }

    @Override
    @Transactional
    public Result<Resource> updateResource(Resource resource) {
        resourceDao.updateResource(resource);
        roleResourceDao.deleteRoleResourceByResourceId(resource.getId());
        if (resource.getRoles() != null) {
            resource.getRoles().stream()
                    .forEach(item -> {roleResourceDao.insertRoleResource(new RoleResource(item.getId(), resource.getId()));});
        }
        return new Result<Resource>(Result.ResultCode.SUCCESS.code, "Update success", resource);
    }

    @Override
    @Transactional
    public Result<Object> deleteResourceById(int id) {
        resourceDao.deleteResourceById(id);
        roleResourceDao.deleteRoleResourceByResourceId(id);
        return new Result<Object>(Result.ResultCode.SUCCESS.code, "Delete success");
    }

    @Override
    public Resource getResourceById(int id) {
        return resourceDao.getResourceById(id);
    }

    @Override
    public List<Resource> getResourcesByRoleId(int roleId) {
        return resourceDao.getResourcesByRoleId(roleId);
    }

    @Override
    public PageInfo<Resource> getResourcesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Resource>(Optional
                .ofNullable(resourceDao.getResourcesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
